package com.scale;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.listener.PageReadListener;
import com.ruoyi.RuoYiApplication;
import com.ruoyi.common.utils.json.JsonUtils;
import com.ruoyi.scale.document.Scale;
import com.ruoyi.scale.model.GrowthNormData;
import com.ruoyi.scale.model.GrowthNormDataWrap;
import com.ruoyi.scale.service.ScaleService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.BeanUtils;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Example;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import java.util.*;


@Deprecated
@RunWith(SpringRunner.class)
@SpringBootTest(classes = RuoYiApplication.class)
public class GrowImport {
    @Resource
    private ScaleService scaleService;
    @Test
    public void startImport() throws Exception {
        Scale queryWhere = new Scale();
        queryWhere.setRuleName("growthEvalRule");

        List<Scale> scales = scaleService.findAll(Example.of(queryWhere));
        for (Scale scale : scales) {
            scaleService.delete(scale);
        }

        String fileName = "F:\\daiscode\\ruoyi-merge\\RuoYi-Vue\\scale\\src\\main\\resources\\儿童体格生长测评-常模-2022.11.28-V4.xlsx";

        //体重-男  体重-女 身高-男  身高-女  身长的体重-男  身长的体重-女  BMI-男  BMI-女  头围-男  头围-女
        Map<String,String> sheetMap = new LinkedHashMap(){
            {
                put("体重-男","boyWeight");
                put("体重-女","girlWeight");
                put("身高-男","boyHeight");
                put("身高-女","girlHeight");
                put("身长的体重-男","boyBodyLength");
                put("身长的体重-女","girlBodyLength");
                put("BMI-男","boyBmi");
                put("BMI-女","girlBmi");
                put("头围-男","boyTw");
                put("头围-女","girlTw");
            }
        };


        Scale scale = new Scale();
        scale.setName("儿童体格生长测评");
        scale.setRuleName("growthEvalRule");
        scale.setDelFlag(0);
        scale.setCode("growth");
        scale.setInputFormClass("com.ruoyi.scale.model.GrowthInput");
       // NormData<GrowthNormDataWrap> growthNormDataWrapNormData = new NormData<>();
        GrowthNormDataWrap wrap = new GrowthNormDataWrap();


        for (Map.Entry<String, String> entry : sheetMap.entrySet()) {
            System.out.println("sheet:" + entry.getKey());

            List<GrowthNormData> rs = new ArrayList<>();
            EasyExcel.read(fileName, GrowExcelVo.class, new PageReadListener<GrowExcelVo>(dataList -> {

                for (GrowExcelVo demoData : dataList) {
                    GrowthNormData data = new GrowthNormData();//ImportAssembler.INSTANCE.toGrowthNormData(demoData);
                    data.setP3(demoData.getP3());
                    data.setP25(demoData.getP25());
                    data.setP50(demoData.getP50());
                    data.setP75(demoData.getP75());
                    data.setP97(demoData.getP97());
                    if(demoData.getAge() != null){
                        data.setKeyVal(demoData.getAge());
                    }
                    if(demoData.getBodyLength() != null){
                        data.setKeyVal(demoData.getBodyLength());
                    }
                    rs.add(data);
                 //   System.out.println(JsonUtils.serialize(data));
                }
            },1000)).sheet(entry.getKey()).doRead();

            BeanUtils.getPropertyDescriptor(GrowthNormDataWrap.class,entry.getValue()).getWriteMethod().invoke(wrap,rs);
        }
//        growthNormDataWrapNormData.setData(wrap);
//        scale.setNormData(growthNormDataWrapNormData);
        scale.setNormData(wrap);
        String serialize = JsonUtils.serialize(scale);
        System.out.println(serialize.length());


//        System.out.println(ClassLayout.parseInstance(scale.getNormData().getData().getBoyBmi().get(0)).toPrintable());
//        System.out.println(ClassLayout.parseInstance(scale.getNormData().getData().getBoyBodyLength().get(0)).toPrintable());


        int size = 0;
        for (Map.Entry<String, String> entry : sheetMap.entrySet()) {
            List list = (List) BeanUtils.getPropertyDescriptor(GrowthNormDataWrap.class,entry.getValue()).getReadMethod().invoke(wrap);
            size +=list.size();
        }

//        long instanceSize = ClassLayout.parseInstance(scale.getNormData().getData().getBoyBodyLength().get(0)).instanceSize();
//        System.out.println("总个数:"+size + " 内存:" + (instanceSize*size));
//        System.out.println();
//
//        System.out.println(ClassLayout.parseInstance(scale).toPrintable());


        System.out.println(JsonUtils.serialize(scale));
        scaleService.save(scale);
    }
    //月龄	P3	P25	P50	P75	P97

}
